/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package RegistrationManager;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;



/**
 *
 * @author maconcon
 */
public class RegistrationServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            Registration newUser=new Registration();
            String password=request.getParameter("password");
          
              String ccnum=request.getParameter("creditcard");
            MessageDigest md=MessageDigest.getInstance("SHA-256");
            md.update(password.getBytes());
            
            byte byteData[]=md.digest();
            
            StringBuffer sb=new StringBuffer();
            for(int i=0;i<byteData.length;i++)
            {
               sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
            }
            
            password=sb.toString();
            
              MessageDigest md1=MessageDigest.getInstance("SHA-256");
            md.update(ccnum.getBytes());
            
            byte byteData1[]=md1.digest();
            
            StringBuffer sb1=new StringBuffer();
            for(int k=0;k<byteData1.length;k++)
            {
               sb1.append(Integer.toString((byteData[k] & 0xff) + 0x100, 16).substring(1));
            }
            
            ccnum=sb1.toString();
            
            
            //  String password1=sb.toString();
                   newUser.setUsername(request.getParameter("username"));
                newUser.setPassword(password);
                newUser.setAddress(request.getParameter("address"));
                newUser.setCity(request.getParameter("city"));
                newUser.setPostal(request.getParameter("postal"));
                newUser.setFirstname(request.getParameter("firstname"));
                newUser.setMiddlename(request.getParameter("middlename"));
                newUser.setLastname(request.getParameter("lastname"));
                newUser.setEmail(request.getParameter("email"));
                newUser.setCreditnumber(ccnum);
                newUser.setCreditname(request.getParameter("ccname"));
                newUser.setExpdate(request.getParameter("expdate"));
                newUser.setCredittype(request.getParameter("credittype"));


                //Add the data to our database
                boolean result = new RegistrationManager().addUser(newUser);

                if (result == true) {
                    response.sendRedirect("success.jsp");
                } else {
                    response.sendRedirect("index.jsp");
                }
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(RegistrationServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        
            
            
            
            
        
        
        
        
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
